﻿package mortal.game.view.copy.groupCopy
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.net.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.game.view.copy.groupCopy.data.*;
    import mortal.game.view.copy.groupCopy.view.*;
    import mortal.game.view.copy.groupCopy.view.groupCopyPanel.*;
    import mortal.game.view.copy.groupCopy.view.singleCopyPanel.*;
    import mortal.game.view.copy.groupCopy.view.vipCopy.*;
    import mortal.game.view.guide.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GroupCopyController extends CopyControllerBase
    {
        private var _groupCopyModule:GroupCopyModule;

        public function GroupCopyController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._groupCopyModule == null)
            {
                this._groupCopyModule = new GroupCopyModule();
                this._groupCopyModule.addEventListener(WindowEvent.SHOW, this.onShow);
                this._groupCopyModule.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._groupCopyModule;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.Copy_LeaveCopy, this.onLeaveCopy);
            NetDispatcher.addCmdListener(ServerCommand.CopyFirstBloodRewardUpdate, this.hasFirstBloodAndShowWin);
            Dispatcher.addEventListener(EventName.GroupCopyOpenByIndex, this.openWin);
            Dispatcher.addEventListener(EventName.GroupCopyOpenByIndexOpenOnly, this.openWinOnyHandler);
            Dispatcher.addEventListener(EventName.AutoGuideOpenCopyTab, this.openGroupCopyHandler);
            Dispatcher.addEventListener(EventName.CopyGroupEnter, this.enterGroupCopy);
            Dispatcher.addEventListener(EventName.GroupCopyListGet, this.getGroupList);
            Dispatcher.addEventListener(EventName.NavbarButtonUIRowInited, this.guideInited);
            Dispatcher.addEventListener(EventName.GroupCopyChangeDefaultIndex, this.changeDefaultIndexHandler);
            Dispatcher.addEventListener(EventName.CopyGroupQuickReq, this.quickGroupHandler);
            Dispatcher.addEventListener(EventName.CopyGroupApplyByBrocast, this.copyGroupApplyByBrocast);
            Dispatcher.addEventListener(EventName.CopyGroupRecruit, this.copyGroupRecruit);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.guideInited);
            EventUtil.addSelfEvent(EventName.GroupCopyGetBoGetRwInfo, this.onGetRwInfo, true);
            EventUtil.addNetEvent(ServerCommand.CopyGroupGetDefenseInfoRes, this.onGetRwInfoBack, true);
            LevelCheckUtil.addInit(this.initHd);
            return;
        }// end function

        private function initHd(param1:Object) : void
        {
            if (GameController.autoGuide.hasGuideDefenseCopy())
            {
                GroupCopyUtil.reqCopyRewInfo();
            }
            return;
        }// end function

        private function changeDefaultIndexHandler(event:DataEvent) : void
        {
            var _loc_2:* = String(event.data);
            Cache.instance.group.groupCopyTemporaryIndex = _loc_2;
            return;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            NetDispatcher.addCmdListener(ServerCommand.GroupCopyTeamInfoGet, this.updateGroupList);
            NetDispatcher.addCmdListener(ServerCommand.GroupPlayerInfoChange, this.updateTeamMate);
            NetDispatcher.addCmdListener(ServerCommand.CopyEnterTimesUpdate, this.updateEnterNum);
            NetDispatcher.addCmdListener(ServerCommand.VipLevelUp, this.updateVipCopyList);
            Dispatcher.addEventListener(EventName.CopyGroupCreate, this.createGroupCopy);
            Dispatcher.addEventListener(EventName.CopyGroupWinClose, this.onCopyGroupClose);
            Dispatcher.addEventListener(EventName.CopyFirstRewardGet, this.copyFirstRewardGet);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.updateTab);
            this.addEs(true);
            return;
        }// end function

        private function onClose(event:WindowEvent) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.GroupCopyTeamInfoGet, this.updateGroupList);
            NetDispatcher.removeCmdListener(ServerCommand.GroupPlayerInfoChange, this.updateTeamMate);
            NetDispatcher.removeCmdListener(ServerCommand.CopyEnterTimesUpdate, this.updateEnterNum);
            NetDispatcher.removeCmdListener(ServerCommand.VipLevelUp, this.updateVipCopyList);
            Dispatcher.removeEventListener(EventName.CopyGroupCreate, this.createGroupCopy);
            Dispatcher.removeEventListener(EventName.CopyGroupWinClose, this.onCopyGroupClose);
            Dispatcher.removeEventListener(EventName.CopyFirstRewardGet, this.copyFirstRewardGet);
            Cache.instance.role.roleEntityInfo.removeEventListener(EntityInfoEventName.UpdateLevel, this.updateTab);
            EventUtil.addNetEvent(EventName.GroupCopyGetBoReward, this.onGetBoRwHd, false);
            EventUtil.addNetEvent(ServerCommand.CopyGroupGetDefenseRwRes, this.onGetBoRwHdBack, false);
            this.addEs(false);
            return;
        }// end function

        private function addEs(param1:Boolean) : void
        {
            EventUtil.addSelfEvent(EventName.GroupCopyGetBoReward, this.onGetBoRwHd, param1);
            EventUtil.addNetEvent(ServerCommand.CopyGroupGetDefenseRwRes, this.onGetBoRwHdBack, param1);
            return;
        }// end function

        private function onGetRwInfo(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.copy.getDefenseDiscardInfo(_loc_2);
            return;
        }// end function

        private function onGetRwInfoBack(event:DataEvent) : void
        {
            if (isViewShow)
            {
                this._groupCopyModule.updateBoRwAfterGet();
            }
            GroupCopyUtil.checkRw();
            return;
        }// end function

        private function onGetBoRwHd(event:DataEvent) : void
        {
            var _loc_2:Object = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (this._groupCopyModule && this._groupCopyModule.isHide == false)
            {
                _loc_2 = event.data;
                _loc_3 = _loc_2.scoreMax;
                _loc_4 = int(event.data.scoreMin);
                GameProxy.copy.getDefenseDiscard(_loc_3, _loc_4);
            }
            return;
        }// end function

        private function onGetBoRwHdBack(event:DataEvent) : void
        {
            if (isViewShow)
            {
                this._groupCopyModule.updateBoRwAfterGet();
            }
            return;
        }// end function

        private function updateTab(event:Event) : void
        {
            if (isViewShow)
            {
                this._groupCopyModule.updateTab();
            }
            return;
        }// end function

        private function guideInited(param1 = null) : void
        {
            if (GameController.autoGuide.hasGuideCopyIcon())
            {
                Cache.instance.role.roleEntityInfo.removeEventListener(EntityInfoEventName.UpdateLevel, this.guideInited);
                Dispatcher.removeEventListener(EventName.NavbarButtonUIRowInited, this.guideInited);
                GroupCopyIcon.instance.show();
            }
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            if (this.isViewShow)
            {
                view.hide();
            }
            if (GroupCopyWaitingView.isViewShow)
            {
                GroupCopyWaitingView.instance.hide();
            }
            if (param1.type == ECopyType._ECopyTypeTeam)
            {
                GroupCopyRightInfoWin.instance.show();
                GroupCopyRightInfoWin.instance.tcopy = param1;
                if (!Cache.instance.buff.isHasBuffByType(EBuffType._EBuffTypeExp))
                {
                    this.guideToUseExpCard();
                }
            }
            else if (param1.type == ECopyType._ECopyTypeVip)
            {
                VipCopyRightInfoWin.instance.show();
                VipCopyRightInfoWin.instance.tcopy = param1;
            }
            else if (param1.type == ECopyType._ECopyTypeSingle)
            {
                SingleCopyRightInfoWin.instance.show();
                SingleCopyRightInfoWin.instance.tcopy = param1;
                if (!Cache.instance.buff.isHasBuffByType(EBuffType._EBuffTypeExp))
                {
                    this.guideToUseExpCard();
                }
            }
            return;
        }// end function

        private function guideToUseExpCard() : void
        {
            var showItem:Function;
            showItem = function (param1:Array) : void
            {
                var _loc_2:* = new GuideUseItemData();
                _loc_2.itemData = param1[0];
                _loc_2.isGuideToUseItem = true;
                _loc_2.isCountdown = false;
                _loc_2.showTxt = Language.getString(35249);
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideShowBackPackItem, _loc_2));
                return;
            }// end function
            ;
            var arr:* = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupProp, EProp._EPropDrug, EDrug._EDrugMoreExp);
            if (arr.length)
            {
                CallLater.setCallLater(showItem, 1, arr[0]);
            }
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            if (AutoFightAI.isWorking)
            {
                AIManager.cancelAll();
            }
            Cache.instance.cd.unregisterCDData(CDDataType.publicCD, "GroupCopyEnterAutoFight");
            return;
        }// end function

        private function onLeaveCopy(param1:TCopy) : void
        {
            if (param1)
            {
                if (param1.type == ECopyType._ECopyTypeTeam)
                {
                    if (GroupCopyRightInfoWin.isViewShow)
                    {
                        GroupCopyRightInfoWin.instance.hide();
                    }
                }
                else if (param1.type == ECopyType._ECopyTypeVip)
                {
                    if (VipCopyRightInfoWin.isViewShow)
                    {
                        VipCopyRightInfoWin.instance.hide();
                    }
                }
                else if (param1.type == ECopyType._ECopyTypeSingle)
                {
                    if (SingleCopyRightInfoWin.isViewShow)
                    {
                        SingleCopyRightInfoWin.instance.hide();
                    }
                }
            }
            return;
        }// end function

        private function openWinOnyHandler(event:DataEvent) : void
        {
            if (GameController.autoGuide.hasGuideCopyIcon())
            {
                if (!isViewShow)
                {
                    view.show();
                }
                this._groupCopyModule.selectedIndex = String(event.data);
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(35145));
            }
            return;
        }// end function

        public function openWin(event:DataEvent) : void
        {
            if (GameController.autoGuide.hasGuideCopyIcon())
            {
                if (this.isViewShow)
                {
                    view.hide();
                }
                else
                {
                    view.show();
                    this._groupCopyModule.selectedIndex = String(event.data);
                }
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(35145));
            }
            return;
        }// end function

        private function openGroupCopyHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupCopyOpenByIndex, _loc_2));
            return;
        }// end function

        private function startCD(event:DataEvent) : void
        {
            var _loc_2:* = Cache.instance.cd.getCDDataByKeyType(CDDataType.publicCD, "GroupCopyEnterAutoFight");
            _loc_2.stopCoolDown();
            _loc_2.startCoolDown();
            return;
        }// end function

        private function autoFight() : void
        {
            if (!AIManager.isWorking)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoFight_A));
            }
            return;
        }// end function

        private function updateGroupList(param1:Object) : void
        {
            this._groupCopyModule.updateGroupList(param1 as SCopyWaitingGroupMsg);
            return;
        }// end function

        private function updateTeamMate(param1:Object) : void
        {
            this._groupCopyModule.updateTeamMate();
            return;
        }// end function

        private function enterGroupCopy(event:DataEvent) : void
        {
            GameProxy.copy.enterCopy(int(event.data));
            return;
        }// end function

        private function onCopyGroupClose(event:DataEvent) : void
        {
            view.hide();
            return;
        }// end function

        private function createGroupCopy(event:DataEvent) : void
        {
            var tcopy:TCopy;
            var str:String;
            var e:* = event;
            tcopy = e.data as TCopy;
            if (Cache.instance.group.isInGroup)
            {
                var closeAlert:* = function (param1:int) : void
            {
                var _loc_2:GroupCopyLeaveOperData = null;
                if (param1 == Alert.OK)
                {
                    if (Cache.instance.group.isCaptain)
                    {
                        if (Cache.instance.group.isTeamMateInCopy)
                        {
                            _loc_2 = new GroupCopyLeaveOperData(GroupCopyLeaveOperData.Create);
                            _loc_2.tcopy = tcopy;
                            _loc_2.isShowAler = false;
                            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupLeave, _loc_2));
                        }
                        else
                        {
                            GameProxy.copy.copyGroupOperate(tcopy.code, ECopyWaitingOperate._ECopyWaitingEnter);
                        }
                    }
                    else
                    {
                        _loc_2 = new GroupCopyLeaveOperData(GroupCopyLeaveOperData.Create);
                        _loc_2.tcopy = tcopy;
                        _loc_2.isShowAler = false;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.GroupLeave, _loc_2));
                    }
                }
                return;
            }// end function
            ;
                if (Cache.instance.group.isCaptain)
                {
                    str = Language.getStringByParam(30817, tcopy.name);
                }
                else
                {
                    str = Language.getStringByParam(30817, tcopy.name);
                }
                Alert.show(str, null, Alert.OK | Alert.CANCEL, null, closeAlert, null, 4);
            }
            else
            {
                GameProxy.copy.copyGroupOperate(tcopy.code, ECopyWaitingOperate._ECopyWaitingEnter);
            }
            return;
        }// end function

        private function getGroupList(event:DataEvent) : void
        {
            GameProxy.copy.copyGroupOperate(event.data.code, ECopyWaitingOperate._ECopyWaitingGetGroup, event.data.page);
            return;
        }// end function

        private function getEnterNum(event:DataEvent) : void
        {
            GameProxy.copy.copyGroupOperate(event.data.code, ECopyWaitingOperate._ECopyWaitingSelfInfo);
            return;
        }// end function

        private function updateEnterNum(param1:Object) : void
        {
            this._groupCopyModule.updateGroupCopyEnterNum();
            return;
        }// end function

        private function quickGroupHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as TCopy;
            GameProxy.copy.copyGroupOperate(_loc_2.code, ECopyWaitingOperate._ECopyWaitingQuickGroup);
            return;
        }// end function

        private function updateVipCopyList(param1:Object) : void
        {
            this._groupCopyModule.updateVipCopyList();
            return;
        }// end function

        private function copyFirstRewardGet(event:DataEvent) : void
        {
            GameProxy.copy.getCopyFirstBlood(event.data.code as int, event.data.item as BaseItem);
            return;
        }// end function

        private function hasFirstBloodAndShowWin(param1:Object) : void
        {
            var _loc_5:TCopy = null;
            var _loc_2:* = Cache.instance.copy.copyFirstBloodDic;
            var _loc_3:String = "";
            var _loc_4:* = CopyConfig.instance.getSingleCopyByLevel(Cache.instance.role.roleInfo.level);
            for each (_loc_5 in _loc_4)
            {
                
                if (_loc_2[_loc_5.code] == ECopyFBType._ECopyFBTypeNotGet)
                {
                    _loc_3 = "m2";
                    break;
                }
            }
            _loc_4 = CopyConfig.instance.getGroupCopyByLevel(Cache.instance.role.roleInfo.level);
            for each (_loc_5 in _loc_4)
            {
                
                if (_loc_2[_loc_5.code] == ECopyFBType._ECopyFBTypeNotGet)
                {
                    _loc_3 = "m1";
                    break;
                }
            }
            if (_loc_3)
            {
                this.view.show();
                this._groupCopyModule.selectedIndex = _loc_3;
            }
            return;
        }// end function

        private function copyGroupRecruit(event:DataEvent) : void
        {
            GameProxy.group.groupRecruit();
            return;
        }// end function

        private function copyGroupApplyByBrocast(event:DataEvent) : void
        {
            var _loc_2:* = event.data.copyCode;
            var _loc_3:* = event.data.entityId as SEntityId;
            var _loc_4:* = CopyConfig.instance.getCopyInfoByCode(_loc_2);
            if (_loc_4)
            {
                if (Cache.instance.role.entityInfo.level < _loc_4.enterMinLevel)
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35250), MsgRollTipsType.msgRollTips3);
                }
            }
            var _loc_5:* = new Array();
            var _loc_6:* = new SGroupOper();
            _loc_6.fromEntityId = Cache.instance.role.entityInfo.entityId;
            _loc_6.toEntityId = _loc_3;
            _loc_6.fromPlayer = new SPublicPlayer();
            _loc_6.fromPlayer.entityId = Cache.instance.role.entityInfo.entityId;
            _loc_6.fromPlayer.guildId = new SEntityId();
            _loc_6.fromPlayer.copyId = new SEntityId();
            _loc_6.type = EGroupOperType._EGroupOperTypeApply;
            _loc_6.copyCode = _loc_2;
            _loc_5.push(_loc_6);
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupApplyOper, _loc_5));
            return;
        }// end function

    }
}
